var login = require('../libs/login.js');
var SendPhoneCode = require('../libs/template/SendPhoneCode.js');
var c = require('../libs/common.js');
var config = require('../libs/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: true,
    regist: false,
    password: true,
    yanjing: '/images/biyan.png',
    status: 1,
    flag: 0,
    color: '#E0E0E0',
    hidden: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(wx.getStorageSync('userinfo'));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  close: function() {
    this.setData({
      phone: ''
    })
  },
  biyan: function(e) {
    var status = e.currentTarget.dataset.status == 1 ? 2 : 1;
    if (status == 2) {
      var yanjing = '/images/zhengyan.png';
      var password = false;
    } else {
      var yanjing = '/images/biyan.png';
      var password = true;
    }
    this.setData({
      password: password,
      yanjing: yanjing,
      status: status
    })
  },
  back:function(){
   wx.switchTab({
     url: '/pages/index/index',
   })
  },
  phonenum: function(e) {
    var phonenum = e.detail.value;
    this.setData({
      phonenum: phonenum
    });
  },
  //输入验证码4位数的时候颜色变亮
  codeinput: function(e) {
    var phonecode = e.detail.value;
    console.log(phonecode);
    if (phonecode.length == 4) {
      this.setData({
        color: '#3ADAA5',
        phonecode: phonecode
      });
    } else {
      this.setData({
        color: '#E0E0E0',
        phonecode: phonecode
      });
    }
  },
  //点击绑定手机弹窗页面“确定”
  userinfo: function(e) {
    if (e.detail.errMsg =='getUserInfo:ok'){
      wx.showLoading({
        title: '登录中...',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
      login.login(e, this);
    };
  },
  //注册手机号码
  registPhone: function(e) {
    var registerPhone = e.detail.value;
    this.setData({
      registerPhone: registerPhone
    });
  },
  //注册密码
  registPassword: function(e) {
    var registPassword = e.detail.value;
    this.setData({
      registPassword: registPassword
    });
  },
  //点击发送验证码按钮--绑定
  sendcode: function() {
    var phonenum = this.data.phonenum;
    SendPhoneCode.sendcode(phonenum, this, 'bindphone');
  },
  //点击发送验证码按钮--注册
  sendcoderegist: function() {
    var phonenum = this.data.registerPhone;
    SendPhoneCode.sendcode(phonenum, this, 'register');
  },
  //获取手机号--登录
  loginphone: function(e) {
    var loginphone = e.detail.value;
    this.setData({
      loginphone: loginphone
    });
  },
  //获取密码--登录
  loginpassword: function(e) {
    var loginpassword = e.detail.value;
    this.setData({
      loginpassword: loginpassword
    });
  },
  //注册里的登录按钮
  loginclick: function() {
    this.setData({
      login: true,
      regist: false
    })
  },
  //获取验证码之后 点击确定--绑定
  sendphonesure: function() {
    var phonecode = this.data.phonecode ? this.data.phonecode : '';
    var phonenum = this.data.phonenum;
    var openid = this.data.openid;
    var userinfo = this.data.userinfo;
    c.phoneVer(phonenum);
    // console.log(phonecode);
    // console.log(phonenum);
    // console.log(openid);
    // console.log(userinfo);
    // console.log(phonecode.length);
    if (phonecode.length == 4) {
      c.ajax(
        config.hostapi + '/Register/xiaoBindPhoneRegister', {
          username: userinfo.nickName,
          head: userinfo.avatarUrl,
          code: phonecode,
          phone: phonenum,
          openid: openid,
          utype: 'user'
        },
        res => {
          if (res.data.code == 100) {
            wx.setStorageSync('userinfo', res.data.data);
            wx.showToast({
              title: '登录成功',
              image: '/images/dui.png'
            })
            setTimeout(function() {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }, 1000)
          } else {
            wx.showToast({
              title: res.data.info,
              image: '/images/closered.png'
            })
          };
        }
      )
    } else {
      wx.showToast({
        title: '验证码格式错误！',
        icon: '',
        image: '/images/closered.png',
        duration: 2000,
        mask: true,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }
  }, //关闭绑定手机弹窗
  closeClick: function() {
    this.setData({
      hidden: true
    });
  },
  //点击新用户按钮切换
  newuser: function() {
    this.setData({
      login: false,
      regist: true
    })
  },
  //注册验证码
  registerPhoneCode: function(e) {
    this.setData({
      registerPhoneCode: e.detail.value
    });
  },
  //注册确定
  regist: function() {
    var registerPhoneCode = this.data.registerPhoneCode;
    var registerPhone = this.data.registerPhone;
    var registPassword = this.data.registPassword;
    c.ajax(
      config.hostapi + '/Register/submitRegister', {
        phone: registerPhone,
        phonecode: registerPhoneCode,
        password: registPassword,
        utype: 'user'
      },
      res => {
        if (res.data.code == 100) {
          wx.showToast({
            title: res.data.info,
          })
          wx.setStorageSync('userinfo', res.data.data);
          setTimeout(function() {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }, 2000);
        } else {
          wx.showToast({
            title: res.data.info,
            image: '/images/closered.png'
          })
        }
      }
    )
    console.log(registerPhoneCode);
    console.log(registerPhone);
    console.log(registPassword);
  },
  //登录
  login: function() {
    wx.showLoading({
      title: '登录中...',
      mask: 'true'
    })
    var loginphone = this.data.loginphone;
    var loginpassword = this.data.loginpassword;
    c.ajax(
      config.hostapi + '/Login/Login', {
        phone: loginphone,
        password: loginpassword,
        utype: 'user'
      },
      res => {
        if (res.data.code == 100) {
          wx.setStorageSync('userinfo', res.data.data);
          wx.showToast({
            title: '登陆成功',
            image: '/images/dui.png'
          })
          setTimeout(function() {
            wx.switchTab({
              url: '/pages/index/index',
            })
          }, 1000)
        } else {
          wx.showToast({
            title: res.data.info,
            image: '/images/closered.png'
          })
        };
      }
    )
  },
  //forgetpassword
  forgetpassword:function(){
    wx.navigateTo({
      url: '/pages/forgetPassword/forgetPassword',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})